import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    meta: { requiresAuth: true }  // 需要登录
  },
  {
    path: '/about',
    name: 'about',
    component: () => import('../views/AboutView.vue')
  },
  {
    path: '/furniture',
    name: 'furniture',
    component: () => import('../views/FurnitureView.vue'),
    meta: { title: '家居管理' }
  },
 /* {
    path: '/furniture/:id',
    name: 'furniture-detail',
    component: () => import('../views/FurnitureDetail.vue'),
    props: true
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/LoginView.vue')
  },*/
  {
    path: '*',
    redirect: '/'  // 404页面重定向
  }
]

const router = new VueRouter({
  mode: 'history',  // 可选：去掉URL中的#号
  base: process.env.BASE_URL,
  routes
})

// 路由守卫示例
router.beforeEach((to, from, next) => {
  // 修改页面标题
  if (to.meta.title) {
    document.title = to.meta.title
  }
  
  // 验证需要登录的路由
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!localStorage.getItem('token')) {
      next({ name: 'login' })
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router